package com.taobao.home.api;


import com.taobao.base.model.PageParams;
import com.taobao.base.model.PageResult;
import com.taobao.base.model.RestResponse;
import com.taobao.home.model.dto.*;
import com.taobao.home.model.po.Goods;
import com.taobao.home.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 添加文档
     */
    @GetMapping("/addGoodsDoc/{goodsId}")
    public RestResponse<String> addGoodsDoc(@PathVariable("goodsId") int goodsId) {
        System.out.println(goodsId);
        return goodsService.addGoodsDoc(goodsId);
    }

    /**
     * 根据Title查询搜素结果页预览商品
     */
    @GetMapping("/getGoodsByTitle/{title}")
    public RestResponse<List<GoodsPreviewDto1>> getGoodsPreviewByTitle(@PathVariable("title") String title) {
        System.out.println(title);
        return goodsService.getGoodsPreviewByTitle(title);
    }

    /**
     * 获取本店铺所有商品
     */
    @GetMapping("/getGoodsByStoreId/{storeId}")
    public RestResponse<List<GoodsPreviewDto1>> getGoodsByStoreId(@PathVariable("storeId") int storeId) {
        System.out.println(storeId);
        return goodsService.getGoodsByStoreId(storeId);
    }
    /**
     * 查询本店铺商品
     */
    @PostMapping("/getStoreIdAndTitle")
    public RestResponse<List<GoodsPreviewDto1>> getStoreIdAndTitle(@RequestBody StoreIdTitle storeIdTitle) {
        System.out.println(storeIdTitle);
        return goodsService.getStoreIdAndTitle(storeIdTitle);
    }

    /**
     * 商品详情页面
     */
    @GetMapping("/getGoodsDetail/{goodsId}")
    public RestResponse<GoodsDetailsDto> getGoodsDetail(@PathVariable("goodsId") int goodsId) {
        System.out.println(goodsId);
        return goodsService.getGoodsDetail(goodsId);
    }
    /**
     * 推荐页商品预览
     */
    @PostMapping("/getGoodsPreview")
    public PageResult<GoodsPreviewDto> getGoodsPreview(@RequestBody PageParams pageParams) {

        System.out.println(pageParams);

        return goodsService.getGoodsPreview(pageParams);
    }

    /**
     * 查询所有
     */
    @GetMapping("/getAllGoods")
    public RestResponse<List<GoodsDto>> getAllGoods() {

        return goodsService.getAllGoods();
    }

    /**
     * 分页条件查询
     */
    @PostMapping("/getGoodsByPage")
    public PageResult<Goods> getGoodsByPage(HttpServletResponse response,
            @RequestBody(required = false) QueryGoodsParamsDto goodsParamsDto) {
        response.setHeader("Access-Control-Allow-0rigin","*");
        PageParams pageParams = new PageParams(goodsParamsDto.getPageNo(), goodsParamsDto.getPageSize());

        System.out.println(pageParams);
        PageResult<Goods> goodsByPage = goodsService.getGoodsByPage(pageParams, goodsParamsDto);

        return goodsByPage;
    }


    /**
     * 添加商品
     */
    @PostMapping("/addGoods")
    public RestResponse<String> addGoods(@RequestBody GoodsDto goodsDto) {

        System.out.println("dsfds");
        return goodsService.addGoods(goodsDto);
    }

    /**
     * 添加商品子分类
     */
    @PostMapping("/addGoodsSubCategory")
    RestResponse<String> addGoodsSubCategory(@RequestBody GoodsSubcategoryDto goodsSubcategoryDto){

        System.out.println(goodsSubcategoryDto);

        return goodsService.addGoodsSubCategory(goodsSubcategoryDto);
    }

    /**
     * 根据商品id查询子分类
     */
    @GetMapping("/getGoodsSubCategoryByGoodsId/{goodsId}")
    RestResponse<List<GoodsSubcategoryDto>> getGoodsSubCategoryByGoodsId(@PathVariable("goodsId") int goodsId){

        System.out.println(goodsId);

        return goodsService.getGoodsSubCategoryByGoodsId(goodsId);
    }

    /**
     * 根据商品id查询可购买数量
     */
    @GetMapping("/getGoodsCount/{list}")
    RestResponse<String> getGoodsCount(@PathVariable("list") List<Integer> list){

        System.err.println(list);

        return goodsService.getGoodsCount(list.get(0), list.get(1)); // 货物id  数量
    }


}
